﻿#region Copyright (c) 2013-04, Olaf Kober <amarok.projects@gmail.com>
//================================================================================
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//================================================================================
#endregion

#if TESTS

using System;
using System.Threading.Tasks;
using NUnit.Framework;


namespace Amarok.Agents
{
	[TestFixture]
	public class Test_AdvertiserExtensions
	{
		private class TestMessage : Message
		{
		}

		private class MockAdvertiser : IAdvertiser
		{
			public Int32 SubscribeAgentCalled;
			public Type SubscribeAgentMessageType;
			public AgentReference SubscribeAgentSubscriber;
			public Predicate<Message> SubscribeAgentMessageFilter;

			public ICompletable Subscribe(Type messageType, AgentReference subscriber, Predicate<Message> messageFilter = null)
			{
				SubscribeAgentMessageType = messageType;
				SubscribeAgentSubscriber = subscriber;
				SubscribeAgentMessageFilter = messageFilter;
				SubscribeAgentCalled++;

				return new DummyCompletable();
			}

			public Int32 SubscribeCallbackCalled;
			public Type SubscribeCallbackMessageType;
			public Action<Message> SubscribeCallbackSubscriber;
			public Predicate<Message> SubscribeCallbackMessageFilter;

			public ICompletable Subscribe(Type messageType, Action<Message> subscriber, Predicate<Message> messageFilter = null)
			{
				SubscribeCallbackMessageType = messageType;
				SubscribeCallbackSubscriber = subscriber;
				SubscribeCallbackMessageFilter = messageFilter;
				SubscribeCallbackCalled++;

				return new DummyCompletable();
			}
		}

		private class StubAgent : IAgent
		{
			public Message PostArgument;
			public Int32 PostCalled;

			public Boolean Post(Message message)
			{
				PostArgument = message;
				PostCalled++;
				return true;
			}

			public Task Completion
			{
				get
				{
					throw new NotImplementedException();
				}
			}

			public void Complete(Boolean discardAvailableMessages = false)
			{
				throw new NotImplementedException();
			}

			public void Dispose()
			{
				throw new NotImplementedException();
			}

			public ICompletable Subscribe(Type messageType, AgentReference subscriber, Predicate<Message> messageFilter = null)
			{
				throw new NotImplementedException();
			}

			public ICompletable Subscribe(Type messageType, Action<Message> subscriber, Predicate<Message> messageFilter = null)
			{
				throw new NotImplementedException();
			}
		}

		private class StubMessageFilter<TMessage>
			where TMessage : Message
		{
			public TMessage InMessage;
			public Int32 FilterCalled;
			public Boolean OutResult = true;

			public Boolean Filter(TMessage message)
			{
				InMessage = message;
				FilterCalled++;
				return OutResult;
			}
		}

		private class MockCallback<TMessage>
			where TMessage : Message
		{
			public TMessage CallbackArgument;
			public Int32 CallbackCalled;

			public void Callback(TMessage message)
			{
				CallbackArgument = message;
				CallbackCalled++;
			}
		}


		// --- TESTS ---


		#region Subscribe<T>(AgentReference)

		[Test]
		public void Subscribe_Agent()
		{
			var advertiser = new MockAdvertiser();
			var agent = new StubAgent();
			var agentReference = agent.ToAgentReference();

			var subscription = advertiser.Subscribe<TestMessage>(
				agentReference);

			Assert.That(subscription, Is.Not.Null);
			Assert.That(advertiser.SubscribeAgentCalled, Is.EqualTo(1));
			Assert.That(advertiser.SubscribeAgentMessageType, Is.EqualTo(typeof(TestMessage)));
			Assert.That(advertiser.SubscribeAgentSubscriber, Is.SameAs(agentReference));
			Assert.That(advertiser.SubscribeAgentMessageFilter, Is.Null);
			Assert.That(agent.PostCalled, Is.EqualTo(0));
			Assert.That(agent.PostArgument, Is.EqualTo(null));

			var message = new TestMessage();
			advertiser.SubscribeAgentSubscriber.Post(message);

			Assert.That(agent.PostCalled, Is.EqualTo(1));
			Assert.That(agent.PostArgument, Is.SameAs(message));
		}

		[Test]
		public void Subscribe_Agent_MessageFilter()
		{
			var advertiser = new MockAdvertiser();
			var agent = new StubAgent();
			var agentReference = agent.ToAgentReference();
			var filter = new StubMessageFilter<TestMessage>();

			var subscription = advertiser.Subscribe<TestMessage>(
				agentReference,
				filter.Filter);

			Assert.That(subscription, Is.Not.Null);
			Assert.That(advertiser.SubscribeAgentCalled, Is.EqualTo(1));
			Assert.That(advertiser.SubscribeAgentMessageType, Is.EqualTo(typeof(TestMessage)));
			Assert.That(advertiser.SubscribeAgentSubscriber, Is.SameAs(agentReference));
			Assert.That(advertiser.SubscribeAgentMessageFilter, Is.Not.Null);
			Assert.That(filter.FilterCalled, Is.EqualTo(0));
			Assert.That(filter.InMessage, Is.EqualTo(null));

			filter.OutResult = true;
			var message = new TestMessage();
			var result = advertiser.SubscribeAgentMessageFilter(message);

			Assert.That(result, Is.True);
			Assert.That(filter.FilterCalled, Is.EqualTo(1));
			Assert.That(filter.InMessage, Is.SameAs(message));

			filter.OutResult = false;
			var message2 = new TestMessage();
			var result2 = advertiser.SubscribeAgentMessageFilter(message2);

			Assert.That(result2, Is.False);
			Assert.That(filter.FilterCalled, Is.EqualTo(2));
			Assert.That(filter.InMessage, Is.SameAs(message2));
		}

		[Test]
		public void Subscribe_Agent_NullAdvertiser()
		{
			var advertiser = (MockAdvertiser)null;
			var agent = new StubAgent();
			var agentReference = agent.ToAgentReference();

			Assert.Throws<NullReferenceException>(() =>
				{
					advertiser.Subscribe<Message>(
						agentReference);
				});
		}

		[Test]
		public void Subscribe_Agent_NullSubscriber()
		{
			var advertiser = new MockAdvertiser();
			var agent = (AgentReference)null;

			Assert.Throws<ArgumentNullException>(() =>
				{
					advertiser.Subscribe<Message>(
						agent);
				});
		}

		#endregion

		#region Subscribe<T>(Action<T>)

		[Test]
		public void Subscribe_Callback()
		{
			var advertiser = new MockAdvertiser();
			var subscriber = new MockCallback<TestMessage>();

			var subscription = advertiser.Subscribe<TestMessage>(
				subscriber.Callback);

			Assert.That(subscription, Is.Not.Null);
			Assert.That(advertiser.SubscribeCallbackCalled, Is.EqualTo(1));
			Assert.That(advertiser.SubscribeCallbackMessageType, Is.EqualTo(typeof(TestMessage)));
			Assert.That(advertiser.SubscribeCallbackSubscriber, Is.Not.Null);
			Assert.That(advertiser.SubscribeCallbackMessageFilter, Is.Null);
			Assert.That(subscriber.CallbackCalled, Is.EqualTo(0));
			Assert.That(subscriber.CallbackArgument, Is.EqualTo(null));

			var message = new TestMessage();
			advertiser.SubscribeCallbackSubscriber(message);

			Assert.That(subscriber.CallbackCalled, Is.EqualTo(1));
			Assert.That(subscriber.CallbackArgument, Is.SameAs(message));
		}

		[Test]
		public void Subscribe_Callback_MessageFilter()
		{
			var advertiser = new MockAdvertiser();
			var subscriber = new MockCallback<TestMessage>();
			var filter = new StubMessageFilter<TestMessage>();

			var subscription = advertiser.Subscribe<TestMessage>(
				subscriber.Callback,
				filter.Filter);

			Assert.That(subscription, Is.Not.Null);
			Assert.That(advertiser.SubscribeCallbackCalled, Is.EqualTo(1));
			Assert.That(advertiser.SubscribeCallbackMessageType, Is.EqualTo(typeof(TestMessage)));
			Assert.That(advertiser.SubscribeCallbackSubscriber, Is.Not.Null);
			Assert.That(advertiser.SubscribeCallbackMessageFilter, Is.Not.Null);
			Assert.That(subscriber.CallbackCalled, Is.EqualTo(0));
			Assert.That(subscriber.CallbackArgument, Is.EqualTo(null));

			filter.OutResult = true;
			var message = new TestMessage();
			var result = advertiser.SubscribeCallbackMessageFilter(message);

			Assert.That(result, Is.True);
			Assert.That(filter.FilterCalled, Is.EqualTo(1));
			Assert.That(filter.InMessage, Is.SameAs(message));

			filter.OutResult = false;
			var message2 = new TestMessage();
			var result2 = advertiser.SubscribeCallbackMessageFilter(message2);

			Assert.That(result2, Is.False);
			Assert.That(filter.FilterCalled, Is.EqualTo(2));
			Assert.That(filter.InMessage, Is.SameAs(message2));
		}

		[Test]
		public void Subscribe_Callback_NullAdvertiser()
		{
			var advertiser = (MockAdvertiser)null;

			Action<TestMessage> subscriber = msg =>
				{
				};

			Assert.Throws<NullReferenceException>(() =>
				{
					advertiser.Subscribe<TestMessage>(
						subscriber);
				});
		}

		[Test]
		public void Subscribe_Callback_NullSubscriber()
		{
			var advertiser = new MockAdvertiser();

			Action<TestMessage> subscriber = null;

			Assert.Throws<ArgumentNullException>(() =>
				{
					advertiser.Subscribe<TestMessage>(
						subscriber);
				});
		}

		#endregion

	}
}

#endif
